package com.ceit.service.impl;

import com.ceit.dao.AdministratorsMapper;
import com.ceit.dao.ManagerMapper;
import com.ceit.dao.UserMapper;
import com.ceit.dao.UserRoleMapper;
import com.ceit.model.*;
import com.ceit.service.AdministratorsS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;

/**
 * Created by 隼 on 2017/2/9.
 */
@Service
public class AdministratorsSImpl implements AdministratorsS {

    @Autowired
    private AdministratorsMapper AdMIN;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private ManagerMapper managerMapper;


    @Override
    public Administrators getadByuserID(Integer userid)throws  Exception {
        AdministratorsExample Example=new AdministratorsExample();
        Example.createCriteria().andUserIdEqualTo(userid);
        List<Administrators> list=AdMIN.selectByExample(Example);
        Iterator<Administrators> iterator=list.iterator();
        Administrators AD=null;
        while (iterator.hasNext()){
            AD=iterator.next();
        }
        return AD;
    }

    @Override
    public List<Administrators> getAdminList()throws  Exception {
        return AdMIN.selectByExample(null);
    }

    @Override
    public Administrators getByAno(String Ano)throws  Exception {
        return AdMIN.selectByPrimaryKey(Ano);
    }

    @Override
    public int getByNum(String Num) throws  Exception{
        UserExample userExample=new UserExample();
        userExample.createCriteria().andNumEqualTo(Num);
        List<User> list=userMapper.selectByExample(userExample);
        Iterator<User> iterator=list.iterator();
        int i=-1;
        if(iterator.hasNext()){
            i=iterator.next().getUserId();
        }
        return i;
    }

    @Override
    public Boolean addUser(String Ano)throws  Exception {
        User user=new User();
        user.setNum(Ano);
        user.setUserPassword("root");
        int i=userMapper.insert(user);
        return (i>0);
    }

    @Override
    public Boolean addADmin(Administrators administrators)throws  Exception {
         int i=AdMIN.insert(administrators);
        return (i>0);
    }

    @Override
    public Boolean addUserRole(int userid, int roleid)throws  Exception {
        UserRole userRole=new UserRole();
        userRole.setUserId(userid);
        userRole.setRoleId(roleid);
        int i=userRoleMapper.insert(userRole);
        return (i>0);
    }

    @Override
    public Boolean updateAD(Administrators administrators) throws Exception {
        int i=AdMIN.updateByPrimaryKey(administrators);
        return (i>0);
    }

    @Override
    public User getUserByuserid(int userid) throws Exception {
        User user=null;
        user=userMapper.selectByPrimaryKey(userid);
        return user;
    }

    @Override
    public Boolean setPassword(String password,int userid) throws Exception {
        User user=null;
        user=userMapper.selectByPrimaryKey(userid);
        int T=-1;
        if(user==null){return  false;}
        else{
            user.setUserPassword(password);
            T=userMapper.updateByPrimaryKey(user);
        }
        return (T>0);
    }

    @Override
    //按照Mno查找管理员
    public Manager getByMno(String Mno) throws Exception {
        return managerMapper.selectByPrimaryKey(Mno);
    }

    @Override
    public Boolean addManage(Manager manager) throws Exception {
        int i=managerMapper.insert(manager);
        return (i>0);
    }


}
